package de.uniulm.muc.mensafinder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MensaFinderActivity extends Activity {
	
	// settings dialog attributes
	static final int DIALOG_SETTINGS_ID = 0;
	private SettingsDbAdapter dbAdapter = new SettingsDbAdapter(this);
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void discovery(View v) {
        Log.i("MensaFinder", "Discovery mode started");
        Intent intent = new Intent(this, DiscoveryActivity.class);
        startActivityForResult(intent, 0);
    }
    
    public void search(View v) {
        Log.i("MensaFinder", "Search mode started");
        Intent intent = new Intent(this, SearchActivity.class);
        startActivityForResult(intent, 1);
    }

    /**
     * Opens a new settings dialog to create/change a username.
     * showDialog calls the onCreateDialog method
     * @param v
     */
    public void settings(View v) {
        Log.i("MensaFinder", "Settings dialog started");
        showDialog(DIALOG_SETTINGS_ID);
    }
    
    /**
	 * Close the App
	 * @param v
	 */
	public void exit(View v) {
	    Log.i("MensaFinder", "MensaFinder finished");
	    this.finish();
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 0) Log.i("MensaFinder", "DiscoveryActivity finished");
        if (resultCode == 1) Log.i("MensaFinder", "SearchActivity finished");
    }

	
	/**
	 * Creates the settings dialog
	 */
	protected Dialog onCreateDialog(int id) {
	    Dialog dialog;
	    switch(id) {
	    case DIALOG_SETTINGS_ID:
	    	dialog = new Dialog(this);
	        dialog.setContentView(R.layout.settings_dialog);
	        dialog.setTitle("MensaFinder Settings");
	        
	        final EditText fieldEdit = (EditText) dialog.findViewById(R.id.editText);
	        dbAdapter.setRowId(-1);
	        fieldEdit.setText(dbAdapter.getUsername());
	        
	        Button saveBtn = (Button) dialog.findViewById(R.id.saveButton);
			Button cancelBtn = (Button) dialog.findViewById(R.id.cancelButton);
			
			saveBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
					if (fieldEdit.getText().length() < 1) return; // edit field is empty
					
					if (fieldEdit.getText().length() < 4 || fieldEdit.getText().length() > 12) {
						showToast("Minimum 4, maximum 12 characters needed!");
						return;
					}
					
					if (dbAdapter.getRowId() == -1) { // create a new user
						dbAdapter.saveUsername(fieldEdit.getText().toString());
					} else {
						dbAdapter.changeUsername(fieldEdit.getText().toString());
					}
					
					removeDialog(DIALOG_SETTINGS_ID);
				}
			});
			
			cancelBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					removeDialog(DIALOG_SETTINGS_ID);
				}
			});
	        
	        break;
	    default:
	        dialog = null;
	    }
	    return dialog;
	}
	
	/**
	 * Creates a toast message
	 * @param text shows this text
	 */
	private void showToast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	}
}